/* 
 * File:	graycode.c
 * Authors: Johan Dahlberg <S102591>
 *				Daniel Jansson <S102496>
 * ---------------------------------
 * Task 1:
 */

#include "genlib.h"
#include "simpio.h"
#include "strlib.h"

/* Prototypes */
/*
 * Function: PrintGrayCode
 * Usage: PrintGrayCode()
 * -----------------
 * Print all possible graycodes
 */
void PrintGrayCode(int nBits);

/*
 * Function: gray
 * Usage: gray()
 * -----------------
 * Handle the straight path
 */
void gray(string prefix, int nBits);


/*
 * Function: yarg
 * Usage: yarg()
 * -----------------
 * Handle the reverse path
 */
void yarg(string prefix, int nBits);

/* Codes */

void graycode() {
	int n;
	
	printf("Enter a number to generate possible graycodes: ");
	n = GetInteger();
	PrintGrayCode(n);

	getchar();
}

void PrintGrayCode(int nBits) {
	gray("", nBits);

	/*if(nBits == 0) {
		printf("%d: 0");
	} else {
		PrintGrayCode(nBits - 1);
		printf("%d: xxx\n", nBits);
	*/
}

void gray(string prefix, int nBits) {
	if(nBits == 0) {
		printf("%s\n", nBits, prefix);
	} else {
		gray(Concat(prefix, "1"), nBits - 1);
		yarg(Concat(prefix, "0"), nBits - 1);
	}
}

void yarg(string prefix, int nBits) {
	if(nBits == 0) {
		printf("%s\n", nBits, prefix);
	} else {
		gray(Concat(prefix, "0"), nBits - 1);
		yarg(Concat(prefix, "1"), nBits - 1);
	}
}